<?php
/**
 * Created by PhpStorm.
 * User: Thans
 * Date: 2018/4/18
 * Time: 下午10:21
 */

namespace app\admin\service;

use app\common\model\ActionModel;
use app\user\model\UserModel;
use think\facade\Cache;
use think\helper\Time;

class CountService
{
    public function newUsers()
    {
        $cache_res = Cache::get('count_new_users_data');
        if($cache_res){
            return ['code' => 1001, 'msg' => 'success', 'data' => $cache_res];
        }
        $user = new UserModel();
        list($start, $end) = Time::today();
        $data['count'] = $user->getNewUsersCountByTimes($start, $end);
        for ($i = 6; $i >= 0; $i--) {
            list($start) = Time::dayToNow($i);
            $data['counts'][] = $user->getNewUsersCountByTimes($start, $start + 60 * 60 * 24);
            $data['keys'][] = date('m-d', $start);
        }
        Cache::set('count_new_users_data', $data, 1800);
        return ['code' => 1001, 'msg' => 'success', 'data' => $data];
    }

    public function userGrowthRate()
    {
        $cache_res = Cache::get('count_user_growth_rate_data');
        if($cache_res){
            return ['code' => 1001, 'msg' => 'success', 'data' => $cache_res];
        }
        $user = new UserModel();
        list($start, $end) = Time::today();
        $data['count'] = ($user->getNewUsersCountByTimes($start, $end) / $user->getNewUsersCountByTimes(0, $end)) * 100;
        for ($i = 6; $i >= 0; $i--) {
            list($start, $end) = Time::dayToNow($i);
            $data['counts'][] = ($user->getNewUsersCountByTimes($start, $start + 60 * 60 * 24) / $user->getNewUsersCountByTimes(0, $end)) * 100;
            $data['keys'][] = date('m-d', $start);
        }
        Cache::set('count_user_growth_rate_data', $data, 1800);
        return ['code' => 1001, 'msg' => 'success', 'data' => $data];
    }

    public function pvOrUv($type = 'pv')
    {
        $cache_res = Cache::get('count_'.$type.'_data');
        if($cache_res){
            return ['code' => 1001, 'msg' => 'success', 'data' => $cache_res];
        }
        $action = new ActionModel();
        $data = $action->pvOrUv($type);
        Cache::set('count_'.$type.'_data', $data, 1800);
        return ['code' => 1001, 'msg' => 'success', 'data' => $data];
    }

    public function device()
    {
        $cache_res = Cache::get('count_device_data');
        if($cache_res){
            return ['code' => 1001, 'msg' => 'success', 'data' => $cache_res];
        }
        $action = new ActionModel();
        $count = $action->pvOrUv('pv')['count'];//所有浏览量
        $mac = $action->getCountByDevice('mac');
        $data[] = ['name' => 'mac', 'value' => $mac];
        $data[] = ['name' => 'android', 'value' => $action->getCountByDevice('android')];
        $data[] = ['name' => 'pc', 'value' => $action->getCountByDevice('pc')];
        $data[] = ['name' => 'wap', 'value' => $action->getCountByDevice('wap')];
        $data[] = ['name' => 'ipad', 'value' => $action->getCountByDevice('ipad')];
        $data[] = ['name' => 'pad', 'value' => $action->getCountByDevice('pad')];
        $data[] = ['name' => 'ios', 'value' => $action->getCountByDevice('ios')];
        $data[] = ['name' => 'windows phone', 'value' => $action->getCountByDevice('windows phone')];
        Cache::set('count_device_data', $data, 1800);
        return ['code' => 1001, 'msg' => 'success', 'data' => $data];
    }

    public function avgUseTime()
    {
        $cache_res = Cache::get('count_avg_use_time_data');
        if($cache_res){
            return ['code' => 1001, 'msg' => 'success', 'data' => $cache_res];
        }
        $action = new ActionModel();
        for ($i = 6; $i >= 0; $i--) {
            list($start) = Time::dayToNow($i);
            $data['counts'][] = $action->getAvgUseTimeByTimes($start, $start + 60 * 60 * 24);
            $data['keys'][] = date('m-d', $start);
        }
        Cache::set('count_avg_use_time_data', $data, 1800);
        return ['code' => 1001, 'msg' => 'success', 'data' => $data];
    }
}